//
//  VisitorView.swift
//  ZMWWeibo
//
//  Created by 张美文 on 16/1/5.
//  Copyright © 2016年 walter. All rights reserved.
//

import UIKit

//定义协议:必须遵守NSObjectProtocol协议
protocol VisitorViewDelegate:NSObjectProtocol{
    //登录的回调
    func loginBtnWillClick()
    //注册的回调
    func registerBtnWillClick()
}

class VisitorView: UIView {
    
    //定义属性保存代理对象, 一定要加上weak，避免循环引用
    weak var delegate: VisitorViewDelegate?
    
    /**
     设置未登录界面
     
     - parameter isHome:    是否是首页
     - parameter imageName: 需要展示的图标
     - parameter message:   需要展示的文本
     */
    func setupVisitorInfo(isHome:Bool, imageName:String, message:String)
    {
        //如果不是首页，就隐藏转盘
        iconView.hidden = !isHome
        //修改中间图标
        homeIcon.image = UIImage(named: imageName)
        //修改文本
        messageLab.text = message
        
        //是首页，执行动画
        if isHome {
            startAnimation()
        }
    }
    
    func loginBtnClick(){
//        print(__FUNCTION__)
        delegate?.loginBtnWillClick()
    }
    
    func registerBtnClick(){
//        print(__FUNCTION__)
        delegate?.registerBtnWillClick()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //1.添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLab)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //2.0 布局子控件
        //2.1 设置背景
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        //2.2 设置小房子
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        //2.3 设置文本
        messageLab.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        //“哪个控件” 的 “什么属性” “等于” “另外一个控件” 的 “什么属性” 乘以 “多少” 加上 “多少”
        let widthCons = NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)
        
        //2.4 设置按钮
        registerBtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLab, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
        
        loginBtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLab, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
        
        //2.5 设置蒙蔽
//        maskBGView.xmg_AlignVertical(type: XMG_AlignType.Center, referView: self, size: nil)
        maskBGView.xmg_Fill(self)
    }

    //Swift推荐我们自定义控件，要么用纯代码，要么xib/stroyboard
    required init?(coder aDecoder: NSCoder) {
        //如果通过xib/stroyboard创建该类，那么就会崩溃
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 内部控制方法
    private func startAnimation(){
        //1. 创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //2. 设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //该属性默认为yes，代表动画只要执行完毕就移除
        anim.removedOnCompletion = false
        //3. 将动画添加到图层上
        iconView.layer.addAnimation(anim, forKey: nil)
    }

    
    // MARK: - 懒加载控件//visitordiscover_feed_image_smallicon
    /// 转盘
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    /// 图标
    private lazy var homeIcon: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    /// 文本
    private lazy var messageLab: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        label.text = "就来得及发了多少解放路大书法家的说法是电缆附件"
        return label
    }()
    /// 登录按钮
    private lazy var loginBtn: UIButton = {
       let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    /// 注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    private lazy var maskBGView: UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}



































































